<template lang="pug">
.wrapper
    scroller
        text.permission {{tip}} 
        text.permission hasUsageAccess : {{hasUsageAccess}}
        text.permission isOpenUsageAccess : {{isOpenUsageAccess}}
        
        text.permission_request(@click="permission_request" v-if="isOpenUsageAccess===false") 请求授权

        text.permission_request(@click="refresh" v-if="isOpenUsageAccess") 刷新 
        
        router-view

        template(v-for="i in appLi")
            .bk
                text.permission {{i[0]}}
                text.permission {{i[1]}}
                text.permission {{i[2]}}
</template>

<script lang="coffee">
weex_import = (s)->
    d = []
    for name in s.split(" ")
        d.push weex.requireModule name
    return d

[Permission, UsageStats, AndroidOs, Hash] = weex_import(\
'Permission UsageStats AndroidOs Hash'
)

SETTING = {
    USAGE_ACCESS : "android.settings.USAGE_ACCESS_SETTINGS"
}

ReloadUsageAccess = ->
    UsageStats.ping (r)=>
        @isOpenUsageAccess = r
        if r
            @refresh()

export default {
    methods:{
        permission_request:->
            Permission.open(SETTING.USAGE_ACCESS, ReloadUsageAccess)

        refresh:->
            now = new Date()
            UsageStats.eventLi(
                now - 600*1000
                now - 0
                (t)=>
                    for i in t
                        i[0] = isotime(i[0])
                    @appLi = t
            )
    }
    data : ->
        
        o = {
            appLi:[]
            hasUsageAccess:0
            isOpenUsageAccess:0
        }

        ReloadUsageAccess.call @

        AndroidOs.meta (r)->
            
            env = weex.config.env
            
            drive_id = [
                r.MANUFACTURER
                r.BRAND
                r.PRODUCT
                r.MODEL
                r.CPU
                env.deviceHeight
                env.deviceWidth
                r.ANDROID_ID
                r.SERIAL
            ].join(' ')

            Hash.b64sha256(
                drive_id
                (h)->
                    console.log h
                    console.log r
                    console.log env
            )
            



        Permission.has SETTING.USAGE_ACCESS, (r)->
            o.hasUsageAccess = r

            
#        weex.requireModule('AppStats').test(
#            (s)->
#                o.tip = "h2o"+s
#        
#        )
        return o
}

isotime = (timestamp)->
  date = new Date(timestamp)
  seperator1 = "-"
  seperator2 = ":"
  month = date.getMonth() + 1
  strDate = date.getDate()
  if month >= 1 and month <= 9
    month = "0" + month
  if strDate >= 0 and strDate <= 9
    strDate = "0" + strDate

  return date.getFullYear() + seperator1 + month + seperator1 + strDate + " " + date.getHours() + seperator2 + date.getMinutes() + seperator2 + date.getSeconds()
</script>

<style lang="scss" scoped>
$blue:#2eb6f9;
.wrapper {
justify-content: center;
align-items: center;
}
.permission {
text-align: center;
margin-top: 70px;
font-size: 35px;
}
.permission_request{
margin:50px 0;
border-style:solid;
border-color:$blue;
border-width:5px;
padding:25px 35px;
color:$blue;
font-size: 35px;
}
.bk{
margin-bottom:20px;
border-bottom-width:1px;
border-style:solid;
border-color:#999;
}
.message {
margin: 30px;
font-size: 32px;
color: #727272;
}
</style>
